/*
 * @Description: EEP检测 - 可用性检测
 * @Author: Rfan
 * @Date: 2022-10-10 14:19:09
 * @LastEditTime: 2022-10-21 18:11:34
 */

import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { Checkbox, Space } from 'antd';
import cs from 'classnames';
import classnames from 'classnames/bind';
import { CheckProps, EnumUsabilityRuleType } from '../../../../../../dictionaries';
import type { IPropsRule } from '../../../../../../interface';
import styles from '../../../styles/main.module.scss';
import { editItemById, insertItem, selectItemByKey } from '@/utils/list';
import { useAddPlanDialogContext } from '@/view/system/ArchivesDetection/components/dialogs/AddPlan/contexts/AddPlanDialogContext';
import { isNullObj } from '@/utils';

const cx = classnames.bind(styles);

// 条目可用性规则列表类型
type TRuleList = IPropsRule<EnumUsabilityRuleType>[];

// 条目可用性完整类型
export interface IUsabilityFullRules {
  ruleType: CheckProps.Usability;
  rules: TRuleList;
}
const EEPUsabilityCheck = (props: any, ref: any) => {
  const { currentRuleInfo, getPropertyRules } = useAddPlanDialogContext();

  // 规则列表
  const [ruleList, setRuleList] = useState<TRuleList>([]);

  useImperativeHandle(ref, () => ({
    save: () => {
      const fullRules: IUsabilityFullRules = {
        ruleType: CheckProps.Usability,
        rules: ruleList
      };
      return fullRules;
    }
  }));

  /**
   * @description: 获取到规则后回写
   */
  useEffect(() => {
    if (currentRuleInfo && !isNullObj(currentRuleInfo)) {
      const ruleObj = getPropertyRules(CheckProps.Usability);
      setRuleList(ruleObj?.rules || []);
    }
  }, [currentRuleInfo]);

  /**
   * @description: 获取规则选中状态
   * @param {EnumUsabilityRuleType} type 真实性检测类型
   * @return {boolean} 选中状态
   */
  const getRuleState = (type: EnumUsabilityRuleType): boolean => {
    const currentRule = selectItemByKey(type, ruleList, 'ruleType');
    return currentRule?.ruleValid;
  };

  /**
   * @description: 设置规则选中状态
   * @param {boolean} valid 选中状态
   * @param {EnumUsabilityRuleType} type 真实性检测类型
   */
  const setRuleState = (valid: boolean, type: EnumUsabilityRuleType) => {
    const currentRule = selectItemByKey(type, ruleList, 'ruleType');
    const editRule = currentRule
      ? editItemById(type, { ...currentRule, ruleValid: valid }, ruleList, 'ruleType')
      : insertItem({ ruleType: type, ruleValid: valid }, ruleList);
    setRuleList(editRule);
  };
  return (
    <>
      <div className={cs(cx('property-wrap'), cx('property-authenticity'))}>
        <Space className={cx('property--title-wrap')} align="center">
          <h6 className={cx('property--title')}>EEP可用性检测</h6>
        </Space>
        <Space direction="vertical">
          <Checkbox
            checked={getRuleState(EnumUsabilityRuleType.EEPContentFormatUsability)}
            onChange={(evt) => {
              setRuleState(evt.target.checked, EnumUsabilityRuleType.EEPContentFormatUsability);
            }}
          >
            内容数据格式长期可用性检测
          </Checkbox>
          <Checkbox
            checked={getRuleState(EnumUsabilityRuleType.EEPBackDataRestorability)}
            onChange={(evt) => {
              setRuleState(evt.target.checked, EnumUsabilityRuleType.EEPBackDataRestorability);
            }}
          >
            备份数据可恢复性检测 （采用专业的备份数据恢复工具检测备份数据是否完好，是否可恢复）
          </Checkbox>
        </Space>
      </div>
    </>
  );
};

export default forwardRef(EEPUsabilityCheck);
